import os
import sys
from PyQt5.QtGui import *
from PyQt5.Qt import *
from pwlf_regression import createNozzleMap
from map_pwlf import fitMap


class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("喷阀坐标工具")
        self.resize(1000, 600)
        self.setup_ui()

    def setup_ui(self):
        layout = QVBoxLayout()  # 界面总的布局是一个垂直布局

        # 相机喷阀映射文件生成器  这个是最上面的标签
        self.label_top = QLabel(self)
        self.label_top.setText("相机喷阀映射文件生成器")
        # self.label.move(180, 10)
        self.label_top.setStyleSheet("font-size:25px;color:black")  # 设置字体和颜色大小
        self.label_top.setAlignment(Qt.AlignCenter)  # 设置字体居中

        #选项卡控件 选择是线性拟合还是分12段拟合
        self.wgt_fit_style=QTabWidget()#wgt_fit_style.tab_linearFit.LF_button3_camera_resolution
        self.wgt_fit_style.tab_linearFit=QWidget()
        self.wgt_fit_style.tab_seg12Fit = QWidget()

        self.wgt_fit_style.addTab(self.wgt_fit_style.tab_seg12Fit, "分12段拟合")
        self.wgt_fit_style.addTab(self.wgt_fit_style.tab_linearFit, "线性拟合")

        #选项卡1的布局*******************************************************
        #线性拟合用垂直布局
        layout_linearFit= QVBoxLayout()
        #然后再往布局里面添加东西
        #添加相机分辨率
        LF_label_camera_resolution = QLabel("相机分辨率(px):")
        self.LF_button1_camera_resolution = QRadioButton("4096")
        self.LF_button2_camera_resolution = QRadioButton("8192")
        self.LF_button3_camera_resolution = QRadioButton("7680")
        self.LF_button3_camera_resolution.setChecked(True)
        self.LF_button4_camera_resolution = QRadioButton("3840")
        self.LF_button5_camera_resolution = QRadioButton("3294")

        layout_linearFit.addWidget(LF_label_camera_resolution)
        layout_linearFit.addWidget(self.LF_button1_camera_resolution)
        layout_linearFit.addWidget(self.LF_button2_camera_resolution)
        layout_linearFit.addWidget(self.LF_button3_camera_resolution)
        layout_linearFit.addWidget(self.LF_button4_camera_resolution)
        layout_linearFit.addWidget(self.LF_button5_camera_resolution)
        # 添加相机分辨率

        #添加其他分辨方案
        LF_label_other_resolution = QLabel("2、其他分辨率方案")
        self.LF_LineEdit_segother_resolution = QLineEdit("")
        layout_linearFit.addWidget(LF_label_other_resolution)#控件造好了 放到layout里面去
        layout_linearFit.addWidget(self.LF_LineEdit_segother_resolution)
        # 添加其他分辨方案

        #添加喷阀总数
        LF_label_penfa_num = QLabel("3、喷阀总数，如256")
        self.LF_LineEdit_segpenfa_num = QLineEdit("256")
        layout_linearFit.addWidget(LF_label_penfa_num)
        layout_linearFit.addWidget(self.LF_LineEdit_segpenfa_num)
        # 添加喷阀总数

        #添加对应的阀序号，以英文输入法","间隔，如：1,20
        LF_label_penfa_sortNumber = QLabel("4、输入对应阀序号，以英文输入法逗号 , 间隔。如:1,20")
        self.LF_LineEdit_segsortNumber = QLineEdit("1,256")
        layout_linearFit.addWidget(LF_label_penfa_sortNumber)
        layout_linearFit.addWidget(self.LF_LineEdit_segsortNumber)
        # 添加对应的阀序号，以英文输入法","间隔，如：1,20

        #输入阀对应的图片坐标
        LF_label_penfa_PicCoordinate = QLabel("5、输入阀对应图片坐标，以英文输入法逗号 , 间隔。如:5,7850")
        self.LF_LineEdit_penfa_PicCoordinate = QLineEdit("5,7850")
        layout_linearFit.addWidget(LF_label_penfa_PicCoordinate)
        layout_linearFit.addWidget(self.LF_LineEdit_penfa_PicCoordinate)
        # 输入阀对应的图片坐标

        #输入不可用的阀序号
        LF_label_penfa_unable = QLabel("6、输入不可用的阀序号(一般是0)")
        self.LF_LineEdit_penfa_unable = QLineEdit("0")
        layout_linearFit.addWidget(LF_label_penfa_unable)
        layout_linearFit.addWidget(self.LF_LineEdit_penfa_unable)
        #然后把这个布局添加到第一个选项卡的布局里面去
        self.wgt_fit_style.tab_linearFit.setLayout(layout_linearFit)
        #选项卡1的布局*******************************************************

        # 选项卡2的布局*******************************************************
        layout_seg12Fit = QVBoxLayout()
        Seg_label= QLabel("-------------------------------------分成1-6段 7-12段-------------------------------------")
        Seg_label.setStyleSheet("font-size:25px;color:red")
        Seg_label.setAlignment(Qt.AlignCenter)
        layout_seg12Fit.addWidget(Seg_label)
        #再做一个widget 然后里面放上网格布局 然后把其他的拿到
        Seg_wgt12=QWidget()
        layout_Seg12 = QGridLayout()
        #layout_Seg12.setSpacing(3)
        for i in range(12):
            layout_Seg12.addWidget(QLabel("分段" + str(i + 1)), 1, i + 1)

        # 为了拿到表格里面的数据 也方便后面使用 最好还是不用for循环 一个一个的赋值吧
        self.LineEdit_seg11 = QLineEdit("1,10")
        layout_Seg12.addWidget(self.LineEdit_seg11, 2, 1)
        self.LineEdit_seg12 = QLineEdit("11,20")
        layout_Seg12.addWidget(self.LineEdit_seg12, 2, 2)
        self.LineEdit_seg13 = QLineEdit("21,30")
        layout_Seg12.addWidget(self.LineEdit_seg13, 2, 3)
        self.LineEdit_seg14 = QLineEdit("31,40")
        layout_Seg12.addWidget(self.LineEdit_seg14, 2, 4)
        self.LineEdit_seg15 = QLineEdit("41,50")
        layout_Seg12.addWidget(self.LineEdit_seg15, 2, 5)
        self.LineEdit_seg16 = QLineEdit("51,60")
        layout_Seg12.addWidget(self.LineEdit_seg16, 2, 6)
        self.LineEdit_seg17 = QLineEdit("1,10")
        layout_Seg12.addWidget(self.LineEdit_seg17, 2, 7)
        self.LineEdit_seg18 = QLineEdit("11,20")
        layout_Seg12.addWidget(self.LineEdit_seg18, 2, 8)
        self.LineEdit_seg19 = QLineEdit("21,30")
        layout_Seg12.addWidget(self.LineEdit_seg19, 2, 9)
        self.LineEdit_seg110 = QLineEdit("31,40")
        layout_Seg12.addWidget(self.LineEdit_seg110, 2, 10)
        self.LineEdit_seg111 = QLineEdit("41,50")
        layout_Seg12.addWidget(self.LineEdit_seg111, 2, 11)
        self.LineEdit_seg112 = QLineEdit("51,60")
        layout_Seg12.addWidget(self.LineEdit_seg112, 2, 12)

        self.LineEdit_seg21 = QLineEdit("0,100")
        layout_Seg12.addWidget(self.LineEdit_seg21, 3, 1)
        self.LineEdit_seg22 = QLineEdit("102,200")
        layout_Seg12.addWidget(self.LineEdit_seg22, 3, 2)
        self.LineEdit_seg23 = QLineEdit("202,300")
        layout_Seg12.addWidget(self.LineEdit_seg23, 3, 3)
        self.LineEdit_seg24 = QLineEdit("303,400")
        layout_Seg12.addWidget(self.LineEdit_seg24, 3, 4)
        self.LineEdit_seg25 = QLineEdit("405,500")
        layout_Seg12.addWidget(self.LineEdit_seg25, 3, 5)
        self.LineEdit_seg26 = QLineEdit("501,600")
        layout_Seg12.addWidget(self.LineEdit_seg26, 3, 6)
        self.LineEdit_seg27 = QLineEdit("0,100")
        layout_Seg12.addWidget(self.LineEdit_seg27, 3, 7)
        self.LineEdit_seg28 = QLineEdit("102,200")
        layout_Seg12.addWidget(self.LineEdit_seg28, 3, 8)
        self.LineEdit_seg29 = QLineEdit("202,300")
        layout_Seg12.addWidget(self.LineEdit_seg29, 3, 9)
        self.LineEdit_seg210 = QLineEdit("303,400")
        layout_Seg12.addWidget(self.LineEdit_seg210, 3, 10)
        self.LineEdit_seg211 = QLineEdit("405,500")
        layout_Seg12.addWidget(self.LineEdit_seg211, 3, 11)
        self.LineEdit_seg212 = QLineEdit("501,600")
        layout_Seg12.addWidget(self.LineEdit_seg212, 3, 12)

        layout_Seg12.addWidget(QLabel("起止阀序号"), 2, 0)
        layout_Seg12.addWidget(QLabel("起止像素值"), 3, 0)

        Seg_wgt12.setLayout(layout_Seg12)
        layout_seg12Fit.addWidget(Seg_wgt12)

        Seg_label2= QLabel("输入起止的两个数字，中间以英文逗号分割，如:5,60")
        Seg_label2.setStyleSheet("font-size:25px;color:black")
        Seg_label2.setAlignment(Qt.AlignCenter)
        layout_seg12Fit.addWidget(Seg_label2)

        Seg_label3= QLabel("-------------------------------------分成1-6段 7-12段-------------------------------------")
        Seg_label3.setStyleSheet("font-size:25px;color:red")
        Seg_label3.setAlignment(Qt.AlignCenter)
        layout_seg12Fit.addWidget(Seg_label3)

        self.wgt_fit_style.tab_seg12Fit.setLayout(layout_seg12Fit)
        # 选项卡2的布局*******************************************************

        #保存路径
        wgt_savePath = QWidget()
        self.layout_savePath = QHBoxLayout()  # 布局里面再放文本框和文本输入框
        self.label_savePath = QLabel("保存路径")
        self.LineEdit_segsavePath = QLineEdit(r"C:\Users\Administrator\Desktop")
        self.btn_savePath = QPushButton("选择路径")
        self.layout_savePath.addWidget(self.label_savePath)
        self.layout_savePath.addWidget(self.LineEdit_segsavePath)
        self.btn_savePath.clicked.connect(self.msg)  # 绑定选择文件路径的槽
        self.layout_savePath.addWidget(self.btn_savePath)
        wgt_savePath.setLayout(self.layout_savePath)
        self.info_savePath = self.LineEdit_segsavePath.text()  # 用一个全局变量接受这个路径


        # Submmit Cancel提交或则取消按钮
        wgt_commit = QWidget()
        self.layout_commit = QHBoxLayout()  # 布局里面再放文本框和文本输入框
        self.btnSubmit_commit = QPushButton("Submit")
        self.btnCancel_commit = QPushButton("Cancel")
        self.layout_commit.addWidget(self.btnSubmit_commit)
        self.layout_commit.addWidget(self.btnCancel_commit)
        self.btnSubmit_commit.clicked.connect(self.getCommit)  # 绑定按钮槽 获取输入的数据
        wgt_commit.setLayout(self.layout_commit)

        #上面的4个控件再放入到外层大布局里面去
        # 相机喷阀映射文件生成器
        layout.addWidget(self.label_top)
        #选项卡控件 选择是线性拟合还是分12段拟合
        layout.addWidget(self.wgt_fit_style)
        # 8、保存路径
        layout.addWidget(wgt_savePath)
        # Submmit Cancel提交或则取消按钮
        layout.addWidget(wgt_commit)
        self.setLayout(layout)


    def msg(self):
        directory1 = QFileDialog.getExistingDirectory(self, "选取文件夹", "./")  # 选择路径
        print("保存路径选择了:\t", directory1)
        self.info_savePath = directory1
        self.LineEdit_segsavePath.setText(self.info_savePath)  # 并且在界面上修改

    def getCommit(self):
        # 1、相机分辨率选择 self.LF_button1_camera_resolution
        if (self.LF_button1_camera_resolution.isChecked() == True):  # 这里是最上面的1、相机分辨率 看选中了哪个框 然后把数字赋值给 nozzleMap 下面刚好对应5个框
            camera_resolution = 4096
        elif (self.LF_button2_camera_resolution.isChecked() == True):
            camera_resolution = 8192
        elif (self.LF_button3_camera_resolution.isChecked() == True):
            camera_resolution = 7680
        elif (self.LF_button4_camera_resolution.isChecked() == True):
            camera_resolution = 3840
        elif (self.LF_button5_camera_resolution.isChecked() == True):
            camera_resolution = 3294
        else:
            camera_resolution = "没有选择"
        print("1、相机分辨率选择 输入了：", camera_resolution, '\t', type(camera_resolution))
        # 2、其他分辨率方案
        info_other_resolution = self.LF_LineEdit_segother_resolution.text()#self.LF_self.LineEdit_segother_resolution
        print("2、其他分辨率方案 输入了：", info_other_resolution if len(info_other_resolution) else "输入为空",
              '\t', type(info_other_resolution), len(info_other_resolution))
        # 3、喷阀总数
        info_penfa_num = self.LF_LineEdit_segpenfa_num.text()#
        print("3、喷阀总数 输入了：", info_penfa_num if len(info_penfa_num) else "输入为空",
              '\t', type(info_penfa_num), len(info_penfa_num))
        # 4、输入了对应阀序号
        info_sortNumber = self.LF_LineEdit_segsortNumber.text()
        print("4、输入了对应阀序号 输入了：", info_sortNumber if len(info_sortNumber) else "输入为空",
              '\t', type(info_sortNumber), len(info_sortNumber))
        # 5、输入阀对应图片坐标
        info_penfa_PicCoordinate =self.LF_LineEdit_penfa_PicCoordinate.text() #self.self.LineEdit_segpenfa_PicCoordinate.text()
        print("5、输入阀对应图片坐标 输入了：", info_penfa_PicCoordinate if len(info_penfa_PicCoordinate) else "输入为空",
              '\t', type(info_penfa_PicCoordinate), len(info_penfa_PicCoordinate))

        # 7、不可用阀序号
        info_penfa_unable = self.LF_LineEdit_penfa_unable.text()
        print("7、不可用阀序号 输入了：", info_penfa_unable if len(info_penfa_unable) else "输入为空",
              '\t', type(info_penfa_unable), len(info_penfa_unable))
        # 8、保存路径
        self.info_savePath=self.LineEdit_segsavePath.text()
        print("8、保存路径 输入了：", self.info_savePath if len(self.info_savePath) else "输入为空",
              '\t', type(self.info_savePath), len(self.info_savePath))

        # 分成12段
        info_seg11 = self.LineEdit_seg11.text()
        info_seg12 = self.LineEdit_seg12.text()
        info_seg13 = self.LineEdit_seg13.text()
        info_seg14 = self.LineEdit_seg14.text()
        info_seg15 = self.LineEdit_seg15.text()
        info_seg16 = self.LineEdit_seg16.text()
        info_seg17 = self.LineEdit_seg17.text()
        info_seg18 = self.LineEdit_seg18.text()
        info_seg19 = self.LineEdit_seg19.text()
        info_seg110 = self.LineEdit_seg110.text()
        info_seg111 = self.LineEdit_seg111.text()
        info_seg112 = self.LineEdit_seg112.text()

        info_seg21 = self.LineEdit_seg21.text()
        info_seg22 = self.LineEdit_seg22.text()
        info_seg23 = self.LineEdit_seg23.text()
        info_seg24 = self.LineEdit_seg24.text()
        info_seg25 = self.LineEdit_seg25.text()
        info_seg26 = self.LineEdit_seg26.text()
        info_seg27 = self.LineEdit_seg27.text()
        info_seg28 = self.LineEdit_seg28.text()
        info_seg29 = self.LineEdit_seg29.text()
        info_seg210 = self.LineEdit_seg210.text()
        info_seg211 = self.LineEdit_seg211.text()
        info_seg212 = self.LineEdit_seg212.text()
        print("---------------分成12段---------------")
        print("第1行输入:", info_seg11 + ' ', info_seg12 + ' ', info_seg13 + ' ', info_seg14 + ' ', info_seg15 + ' ',
              info_seg16 + ' ',
              info_seg17 + ' ', info_seg18 + ' ', info_seg19 + ' ', info_seg110 + ' ', info_seg111 + ' ', info_seg112)
        print("第2行输入:", info_seg21 + ' ', info_seg22 + ' ', info_seg23 + ' ', info_seg24 + ' ', info_seg25 + ' ',
              info_seg26 + ' ',
              info_seg27 + ' ', info_seg28 + ' ', info_seg29 + ' ', info_seg210 + ' ', info_seg211 + ' ', info_seg212)
        print("---------------分成12段---------------")

        # 下面开始转后面的逻辑运算代码 分两种情况 一个是线性拟合 一个是全部拟合 、
        # 具体就是  print("当前选项卡:",self.wgt_fit_style.currentIndex()) 是0还是1
        fit_style=self.wgt_fit_style.currentIndex()#通过拿到选显卡的索引去拿到对应的模式  0就是线性拟合 1就是分段拟合
        print("当前选项卡:", fit_style)
        status = False
        if fit_style==0:#这个时候就是分段拟合 然后就是拿到那24个数字 然后进行处理
            savePath = self.info_savePath  # 拿到保存路径
            infoSegIut = [info_seg11, info_seg12, info_seg13, info_seg14, info_seg15, info_seg16,
                          info_seg17, info_seg18, info_seg19, info_seg110, info_seg111, info_seg112,
                          info_seg21, info_seg22, info_seg23, info_seg24, info_seg25, info_seg26,
                          info_seg27, info_seg28, info_seg29, info_seg210, info_seg211, info_seg212]
            #输入的是一对儿  11和21是一对儿 12和22是一对儿 要么都输入 要么都不输入
            infoSegPair=[[info_seg11,info_seg21],[info_seg12,info_seg22],[info_seg13,info_seg23],[info_seg14,info_seg24],
                         [info_seg15,info_seg25],[info_seg16,info_seg26],[info_seg17,info_seg27],[info_seg18,info_seg28],
                         [info_seg19,info_seg29],[info_seg110,info_seg210],[info_seg111,info_seg211],[info_seg112,info_seg212]]

            for inPutPair in infoSegPair:
                #如果两个都为空 那就是True
                if len(inPutPair[0])==0 and len(inPutPair[1])==0:
                    status=True
                elif len(inPutPair[0])!=0 and len(inPutPair[1])!=0:#如果两个都不为空 判断是不是有逗号
                    for inputP in inPutPair:
                        if "," not in inputP:#如果没有逗号那就是错
                            print("数字之间没有输入逗号")
                            status = False
                            break
                        else:
                            status = True
                else:#这种情况就是一个为空 另外一个不为空的情况 那就是false
                    status=False

            # for infoInput in infoSegIut:#可能是没有输入 就是空的
            #     if "," not in infoInput:
            #         print("数字之间没有输入逗号")
            #         status=False
            #     elif "," in infoInput:#如果有逗号 检查两边的数字 len是否大于0
            #         oneInputList=infoInput.split(',')#list(map(int, infoInput.split(',')))
            #         for numberStr in oneInputList:
            #             if len(numberStr)>0:#数字长度大于0是正常的 否则就是异常的
            #                 status = True
            #             else:
            #                 status=False

            if status:  # 如果输入的数据格式是无误的  就开始分割转成int类型 并且每两个一组
                # infoSeg = [[1, 20], [21, 40], [41, 60], [61, 80], [81, 100], [101, 120],
                #            [121, 140], [141, 160], [161, 180], [181, 200], [201, 220], [221, 240],
                #            [5, 1001], [5, 1002], [5, 1003], [5, 1004], [5, 1005], [5, 1006],
                #            [5, 1007], [5, 1008], [5, 1009], [5, 1010], [5, 1011], [5, 1012]]
                statusList = []
                infoSeg=[]
                for infoInput in infoSegIut:#遍历输入的文本框里面的内容 从11 12 13...211 212
                    if len(infoInput)>0:
                        oneIntList=list(map(int, infoInput.split(','))) #按照逗号分割并且转成整形 并且放在一个list里面
                        infoSeg.append(oneIntList)
                print("infoSeg:",infoSeg)

                filename6 = '1-6段'
                if len(infoSeg) ==12:#这里12的意思是 6对儿
                    #先把原来的给删掉
                    if os.path.exists(savePath + "\\" + filename6 + '.txt'): os.remove(savePath + "\\" + filename6 + '.txt')
                    for i in range(0, 6, 1):#按照一对儿输入 11和21  从列表索引来看就是 infoSeg的第0个元素和第12个元素
                        print(infoSeg[i], type(infoSeg[i]), infoSeg[i + 6], type(infoSeg[i + 6]))
                        #如果是第一段 暂时不考虑 从第二段开始 判断一下输入的像素是不是和上一段像素有间隙的像素如[0,100] [102,200] 中间差了两个像素 这个时候需要补全
                        #补全的操作是这样的，把txt文件读取进来，然后把最后n个字符再补充写入就OK了
                        if i>0:
                            n=infoSeg[i + 6][0]-infoSeg[i-1 + 6][1]#看中间的缝隙有多大 有几个 然后再补充吧
                            if n>0:
                                with open(savePath + "\\" + filename6 + '.txt','r') as f_r:
                                    content=f_r.read()#先把原来的txt读取进来，然后再把最后n个字符拿到 再写进去
                                    strBu=' '.join(content.replace(' ','')[-n:])#要补充的字符
                                    with open(savePath + "\\" + filename6 + '.txt', 'a+') as f_w:
                                        f_w.write(strBu+' ')

                        status1 = fitMap(infoSeg[i], infoSeg[i + 6], savePath,filename6)
                        statusList.append(status1)
                    print("statusList:", statusList)
                    if sum(statusList) == 6:
                        status = True
                    # with open(savePath + '//'+filename6+'.txt', 'r', encoding='utf-8') as f_old:
                    #     content = f_old.read()
                    #     content = content.replace('\n', ' ')
                    #     with open(savePath + '//map1到6.txt', 'w', encoding='utf-8') as f_new:
                    #         f_new.write(content)
                    #os.remove(savePath + '//'+filename6+'.txt')

                if len(infoSeg)>12:
                    filename12 = '7-12段'
                    if os.path.exists(savePath + "\\" + filename6 + '.txt'): os.remove(savePath + "\\" + filename6 + '.txt')
                    if os.path.exists(savePath + "\\" + filename12 + '.txt'): os.remove(savePath + "\\" + filename12 + '.txt')
                    for i in range(0, 6, 1):#按照一对儿输入 11和21  从列表索引来看就是 infoSeg的第0个元素和第12个元素
                        print(infoSeg[i], type(infoSeg[i]), infoSeg[i + 12], type(infoSeg[i + 12]))

                        if i>0:
                            n=infoSeg[i + 12][0]-infoSeg[i-1 + 12][1]#看中间的缝隙有多大 有几个 然后再补充吧
                            if n>0:
                                with open(savePath + "\\" + filename6 + '.txt','r') as f_r:
                                    content=f_r.read()#先把原来的txt读取进来，然后再把最后n个字符拿到 再写进去
                                    strBu=' '.join(content.replace(' ','')[-n:])#要补充的字符
                                    with open(savePath + "\\" + filename6 + '.txt', 'a+') as f_w:
                                        f_w.write(strBu+' ')

                        status1 = fitMap(infoSeg[i], infoSeg[i + 12], savePath,filename6)
                        statusList.append(status1)

                    for i in range(6, 12, 1):#按照一对儿输入 11和21  从列表索引来看就是 infoSeg的第0个元素和第12个元素
                        print(infoSeg[i], type(infoSeg[i]), infoSeg[i + 12], type(infoSeg[i + 12]))
                        if i>6:
                            n = infoSeg[i + 12][0] - infoSeg[i - 1 + 12][1]  # 看中间的缝隙有多大 有几个 然后再补充吧
                            if n > 0:
                                with open(savePath + "\\" + filename12 + '.txt', 'r') as f_r:
                                    content = f_r.read()  # 先把原来的txt读取进来，然后再把最后n个字符拿到 再写进去
                                    strBu = ' '.join(content.replace(' ', '')[-n:])  # 要补充的字符
                                    with open(savePath + "\\" + filename12 + '.txt', 'a+') as f_w:
                                        f_w.write(strBu+' ')

                        status1 = fitMap(infoSeg[i], infoSeg[i + 12], savePath,filename12)
                        statusList.append(status1)
                    print("statusList:", statusList)
                    if sum(statusList) == 12:
                        status = True

                    # for i in range(0, 6, 1):#按照一对儿输入 11和21  从列表索引来看就是 infoSeg的第0个元素和第12个元素
                    #     print(infoSeg[i], type(infoSeg[i]), infoSeg[i + 12], type(infoSeg[i + 12]))
                    #     status1 = fitMap(infoSeg[i], infoSeg[i + 12], savePath,filename6)
                    #     statusList.append(status1)
                    # print("statusList:", statusList)
                    # if sum(statusList) == 6:
                    #     status = True
                    # with open(savePath + '//'+filename6+'.txt', 'r', encoding='utf-8') as f_old:
                    #     content = f_old.read()
                    #     content = content.replace('\n', ' ')
                    #     with open(savePath + '//map1到6.txt', 'w', encoding='utf-8') as f_new:
                    #         f_new.write(content)
                    # os.remove(savePath + '//'+filename6+'.txt')
                    #
                    # for i in range(6, 12, 1):#按照一对儿输入 11和21  从列表索引来看就是 infoSeg的第0个元素和第12个元素
                    #     print(infoSeg[i], type(infoSeg[i]), infoSeg[i + 12], type(infoSeg[i + 12]))
                    #     status1 = fitMap(infoSeg[i], infoSeg[i + 12], savePath,filename12)
                    #     statusList.append(status1)
                    # print("statusList:", statusList)
                    # if sum(statusList) == 6:
                    #     status = True
                    # with open(savePath + '//'+filename12+'.txt', 'r', encoding='utf-8') as f_old:
                    #     content = f_old.read()
                    #     content = content.replace('\n', ' ')
                    #     with open(savePath + '//map7到12.txt', 'w', encoding='utf-8') as f_new:
                    #         f_new.write(content)
                    # os.remove(savePath + '//'+filename12+'.txt')

        else:#线性拟合 这里只有两个选项卡 不是线性拟合（下标为1 那就是下标0）    直接把原来的代码给拿过来就OK了
            if (len(info_penfa_num)==0 or len(info_sortNumber)==0):
                print("喷阀数量或输入的阀序号为空")
                exit()
            #如果输入的喷阀总数和对应的喷阀序号不是空的（一般都是数字）
            nuzzleSeq = info_sortNumber.split(',')  # 把输入的喷阀序号按照逗号分割出来 这个时候是一个含有两个字符元素的list
            nuzzleSeq = list(map(int, nuzzleSeq))  # 把value7即输入的喷阀序号按照逗号分割之后的元素全部转为整形 然后组成一个list
            pixelSeq = info_penfa_PicCoordinate.split(',')#输入的喷阀对应的图片坐标也做同样的处理 即使把输入的两个字符转成两个整型 放在一个list里
            pixelSeq = list(map(int, pixelSeq))  # value8也是这样
            #上面这两个东西 就是要映射的两个文件的端点

            nozzleMap = camera_resolution#这里是输入的相机分辨率 天元的设备一般是7680
            #下面这句代码 就是其他分辨率方案的情况 暂时没有用上
            if info_other_resolution!= '':#如果value5不是空的 也就是其他分辨率方案不是空的 就把里面的新值赋值给 nozzleMap，不过这个循环一般进不来吧！其他分辨率一般是为空
                nozzleMap=int(info_other_resolution)#不过目前的 其他分辨率方案info_other_resolution是空的 没有输入值

            #然后打印一下nozzleMap 也就是nozzleMap要么是选择的相机分辨率 要么是输入的新的分辨率 不过暂时用不到新的分辨率
            print("nozzleMap:",nozzleMap)  # 打印一下nozzleMap  以上算是通过前两行得到了nozzleMap这个变量 是整形的一个数字　　天元设备一般是７６８０

            nuzzleNumTotal = info_penfa_num  # 拿到V6也就是 喷阀总数的输入
            badNozzle = info_penfa_unable  # 拿到不可用的阀序号 也就是value11
            savePath = self.info_savePath  # 拿到保存路径

            # 以上的变量都拿到以后 然后把上面的变量放入到下面的函数里面 输出 status
            #createNozzleMap输入：pixelSeq，也就是输入的喷阀对应的图片坐标的list如[5,7650]，
                              #  nuzzleSeq，输入对应的喷阀序号组成的list，如[1,256]
                              #  int(nuzzleNumTotal)   输入的喷阀总数（转为整形）
                              #  savePath 就是保存文件的路径
                              #  nozzleMap 就是上面的相机分辨率  天元的选择的就是7680 或者手动输入的其他方案
                              #  curzeType 就是上面的拟合方式 暂时目前用的都是现行拟合
                              #  int(badNozzle) 就是不可用的阀的序号（转为整形）
            #以上就是这几个函数的输入的参数 后面就是去拟合了
            status = createNozzleMap(pixelSeq, nuzzleSeq, int(nuzzleNumTotal), savePath, nozzleMap, True,
                                     int(badNozzle))
        if status:
            dialog = QDialog()
            dialog.resize(200, 100)
            dialog.setWindowTitle("显示生成结果")
            label = QLabel(dialog)
            label.setText("生成成功")
            dialog.setWindowModality(Qt.ApplicationModal)
            dialog.exec()
            print('生成成功!')
        else:
            dialog = QDialog()
            dialog.resize(200, 100)
            dialog.setWindowTitle("显示生成结果")
            label = QLabel(dialog)
            label.setText("生成失败，请检查输入的数据格式")
            dialog.setWindowModality(Qt.ApplicationModal)
            dialog.exec()
            print('生成失败!')

if __name__ == '__main__':
    app=QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())